const downLoadModel = appRequire('models/downLoad');
const common = appRequire('common/common');

// 导出对象，供其它模块调用
module.exports = {
    downLoadService,
    list,
};

//下载列表
async function list(req, request) {
    let postData = req.body
    const returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let result1 = Common.checkUrl(postData, ['uid'], request);
    if (result1 == false) return;

    let result2 = await downLoadModel.downLoad.findAndCountAll({
        where: {
            userId: postData.uid,
        },
        include: ["artPPT"],
        order: [['createdAt', 'DESC']],
        limit: postData.pageSize,
        offset: postData.pageSize * (postData.pageNum - 1)
    })

    result2 = Common.clone(result2)
    //console.log(artppt)
    result2.rows = await Common.transfromDataList(result2.rows)

    returnData.maxCount = result2.count
    returnData.maxPage = Math.ceil(result2.count / postData.pageSize)
    returnData.data = result2.rows

    await request.json(returnData)
}

//下载
async function downLoadService(req) {
    let postData = req.body

    let params = {
        userId: postData.uid,
    }
    if (postData.artpptId) params.artpptId = postData.artpptId
    let result2 = await downLoadModel.downLoad.findOne({
        where: params
    })
    if (result2) {
        /*let result3 = await downLoadModel.downLoad.update({
            createdAt: dateFormat(Date.parse(new Date()), 'yyyy-mm-dd HH:MM:ss')
        }, {
            where: {
                objectId: postData.objectId
            }
        })*/
    } else {
        let params = {
            objectId: sid.uuid(),
            userId: postData.uid,
        }
        if (postData.artpptId) params.artpptId = postData.artpptId
        let result3 = await downLoadModel.downLoad.create(params)
    }
    return true
}
